package com.baizhi.service.impl;

import com.baizhi.dao.FeedDao;
import com.baizhi.dto.VideoFeedDTO;
import com.baizhi.entity.Feed;
import com.baizhi.feign.VideoFeign;
import com.baizhi.po.FeedPO;
import com.baizhi.po.FeedVideoPO;
import com.baizhi.po.FeedsUserPO;
import com.baizhi.service.FeedService;
import com.baizhi.service.UserService;
import com.baizhi.util.AliyunOSSUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 动态(Feed)表服务实现类
 *
 * @author makejava
 * @since 2021-09-27 09:35:41
 */
@Transactional
@Service("feedService")
public class FeedServiceImpl extends ServiceImpl<FeedDao, Feed> implements FeedService {
    //存储地址
    private final static String bucketName = "bzkt-2021-chenjf";
    private static final Logger log1 = LoggerFactory.getLogger(FeedServiceImpl.class);
    @Autowired
    private FeedDao feedDao;
    @Autowired
    private UserService userService;
    @Autowired
    private VideoFeign videoFeign;


    @Override
    public List<FeedPO> queryAllFeed(Integer page, Integer per_page) {
        //分页
        Page<Feed> page1 = new Page<>(page, per_page);
        //条件对象
        LambdaQueryWrapper<Feed> queryWrapper = new LambdaQueryWrapper<>();
        //条件
        queryWrapper.orderByDesc(Feed::getCreatedAt);

        Page<Feed> feedPage = feedDao.selectPage(page1, queryWrapper);
        //获取条件后的数据
        List<Feed> records = feedPage.getRecords();
        List<FeedPO> feedPOS = new ArrayList<>();
        records.forEach(feed -> {
            //根据用户id找到用户，并根据用户获取数据,
            Integer userId = feed.getUserId();
            FeedsUserPO feedsUserPO = userService.queryByUserId(userId);
            //根据视频id获取视频的数据
            Integer videoId = feed.getVideoId();
            FeedVideoPO feedVideoPO = null;
            if (videoId != null) {
                feedVideoPO = videoFeign.queryByVideosId(videoId);
            }


            //拆分图片，按空格拆分,放入到集合中
            String images = feed.getImages();
            String[] s = null;
            List<String> strings = new ArrayList<>();
            if (images != null) {
                s = images.split(" ");
                for (String s1 : s) {
                    strings.add(s1);
                    log1.info("图片为 {}", s1);
                }
            }
            FeedPO feedPO = new FeedPO();
            BeanUtils.copyProperties(feed, feedPO);
            feedPO.setImages(strings);
            feedPO.setUser(feedsUserPO);
            feedPO.setVideo(feedVideoPO);
            feedPOS.add(feedPO);
        });
        return feedPOS;

    }

    @Override
    public void insert(List<MultipartFile> file, String content, Integer uid) {
        List<String> strings = new ArrayList<>();
        String path = " https://bzkt-2021-chenjf.oss-cn-hangzhou.aliyuncs.com/";
        //处理文件上传
        if (file != null) {
            //是图片在上传，否则为null
            for (MultipartFile multipartFiles : file) {
                String contentType = multipartFiles.getContentType();
                System.out.println("contentType = " + contentType);
                String[] split = contentType.split("/");
                log1.info("split[0] {}", split[0]);
                if (split[0].equals("image")) {
                    for (MultipartFile multipartFile : file) {
                        //单个文件
                        String newFileName = System.currentTimeMillis() + "_" + multipartFile.getOriginalFilename();
                        AliyunOSSUtil.uploadfileBytes(bucketName, "image/" + newFileName, multipartFile);
                        strings.add(newFileName);
                    }
                }
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (String string : strings) {
            stringBuilder.append(path + string);
            stringBuilder.append(" ");
        }
        String images = stringBuilder.toString();
        log1.info("images {}", images);
        Feed entity = new Feed();
        entity.setUserId(uid);
        entity.setType("normal");
        entity.setContent(content);
        entity.setImages(images);
        entity.setCreatedAt(new Date());
        entity.setUpdatedAt(new Date());
        feedDao.insert(entity);
    }

    @Override
    public void insertVideo(VideoFeedDTO videoFeedDTO) {
        Feed entity = new Feed();
        entity.setUserId(videoFeedDTO.getUserId());
        entity.setType("video");
        entity.setVideoId(videoFeedDTO.getVideoId());
        entity.setContent(videoFeedDTO.getContent());
        entity.setCreatedAt(new Date());
        entity.setUpdatedAt(new Date());
        int insert = feedDao.insert(entity);
    }

}

